package com.treasure.mall.biz.repository.live;

import com.treasure.mall.biz.base.JpaSearchableRepository;
import com.treasure.mall.biz.entity.live.LiveRoomUserPO;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

/**
 * 
 * <p>
 * +---------------------------------------------------------
 * * @package: com.treasure.mall.biz.repository.live
 * * @author: guomw (guomwchen@foxmail.com)
 * * @date: 2020/9/16 6:20 下午
 * * @since: V1.0.0
 * +---------------------------------------------------------
 * 注意：本内容仅限于内部传阅，禁止外泄以及用于其他的商业目的
 * 
 */
@Repository
public interface LiveRoomUserRepository extends JpaSearchableRepository<LiveRoomUserPO, Long> {

    /**
     * 获取直播间用户信息
     *
     * @param userId    用户ID
     * @param sessionId 直播间场次ID
     * @return
     */
    LiveRoomUserPO findFirstByUser_UserIdAndSessionId(Long userId, String sessionId);


    /**
     * 检查用户是否被禁言
     *
     * @param sessionId 直播间场次ID
     * @param userId    用户ID
     * @return
     */
    long countBySessionIdAndUser_UserIdAndForbidIsTrue(String sessionId, Long userId);


    /**
     * 用户禁言
     *
     * @param sessionId 直播间
     * @param userId    用户
     * @param forbid    是否禁言
     */
    @Query("update LiveRoomUserPO po set po.forbid =?3 where po.sessionId=?1 and po.user.userId=?2")
    @Modifying
    @Transactional(rollbackFor = Exception.class)
    void modifyUserForbid(String sessionId, Long userId, boolean forbid);

    /**
     * 用户禁言
     *
     * @param id     直播用户列表住建id
     * @param forbid 是否禁言
     */
    @Query("update LiveRoomUserPO po set po.forbid =?2 where po.id=?1")
    @Modifying
    @Transactional(rollbackFor = Exception.class)
    void modifyUserForbid(Long id, boolean forbid);


    /**
     * 设置用户上线，累计观看次数
     *
     * @param sessionId 直播间
     * @param userId    用户
     */
    @Query("update LiveRoomUserPO po set po.online = true,po.enterCount=po.enterCount+1 where po.sessionId=?1 and po.user.userId=?2")
    @Modifying
    @Transactional(rollbackFor = Exception.class)
    void modifyUserOnline(String sessionId, Long userId);

    /**
     * 设置用户上线，不计观看次数
     *
     * @param sessionId
     * @param userId
     */
    @Query("update LiveRoomUserPO po set po.online =true where po.online=false and po.sessionId=?1 and po.user.userId=?2")
    @Modifying
    @Transactional(rollbackFor = Exception.class)
    void modifyUserOnlineNoEnterCount(String sessionId, Long userId);

    /**
     * 设置用户下线
     *
     * @param sessionId
     * @param userId
     * @param lastLeaveTime
     */
    @Query("update LiveRoomUserPO po set po.online =false,po.lastLeaveTime=?3 where po.online=true and po.sessionId=?1 and po.user.userId=?2")
    @Modifying
    @Transactional(rollbackFor = Exception.class)
    void modifyUserOffline(String sessionId, Long userId, LocalDateTime lastLeaveTime);


    /**
     * 设置用户在线时长
     *
     * @param sessionId 直播间场次
     * @param userId    用户
     * @param duration  时长
     */
    @Query("update LiveRoomUserPO po set po.userOnlineTotalSeconds=po.userOnlineTotalSeconds+?3 where po.sessionId=?1 and po.user.userId=?2")
    @Modifying
    @Transactional(rollbackFor = Exception.class)
    void modifyUserOnlineDuration(String sessionId, Long userId, Long duration);
}
